package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.CouponAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.bean.CouponBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.jucai.pppeiyouclient.base.BaseApplication.getContext;

public class WalletCouponActivity extends BaseActivity implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "WalletCouponActivity : ";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView couponsRv;
    private List<CouponBean> coupons = new ArrayList<>();
    private CouponAdapter couponAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_activity_with_ptr);

        couponAdapter = new CouponAdapter(WalletCouponActivity.this, coupons);
        ((TextView) findViewById(R.id.common_title_text)).setText("优惠券");
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        couponsRv = (PullToRefreshForRecyclerView) findViewById(R.id.content_view);
        couponsRv.setLayoutManager(new LinearLayoutManager(WalletCouponActivity.this, LinearLayoutManager.VERTICAL, false));
        couponsRv.setItemAnimator(new DefaultItemAnimator());
        couponsRv.setAdapter(couponAdapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Find_Coupon.getlist");
        param.put("member_id", LoginUtils.getMember_id(getContext()));
        param.put("token", LoginUtils.getToken(getContext()));

        CommonRequestNetData.getJsonArrayData(WalletCouponActivity.this, param,true, new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                LogUtils.e(TAG + "jsonArray = " + jsonArray.toString());
                coupons = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<CouponBean>>() {
                }.getType());
                LogUtils.e("coupons = " + coupons.toString());
                couponAdapter.setData(coupons);
            }
        });
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
            case R.id.common_home_back:
                startActivity(new Intent(WalletCouponActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initData();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
        refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initData();
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
        refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
    }
}
